package com.example.leetdemo.finash;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Solution36 {

    public boolean isValidSudoku(char[][] board) {
        Map<Integer, Set<Character>> rows = new HashMap<>();
        Map<Integer, Set<Character>> lines = new HashMap<>();
        Map<Integer, Set<Character>> table = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            rows.put(i, new HashSet<>());
            lines.put(i, new HashSet<>());
            table.put(i, new HashSet<>());
        }
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if (board[i][j] == '.') {
                    continue;
                }
                int m = (i / 3 + 1) + (j / 3) * 3 ;
                if (rows.get(i).contains(board[i][j])
                        || lines.get(j).contains(board[i][j])
                        || table.get(m).contains(board[i][j])) {
                    return false;
                }
                rows.get(i).add(board[i][j]);
                lines.get(j).add(board[i][j]);
                table.get(m).add(board[i][j]);
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Solution36 solution36 = new Solution36();
        char[][] chas = new char[][] {
                {'5','3','.','.','7','.','.','.','.'},
                {'6','.','.','1','9','5','.','.','.'},
                {'.','9','8','.','.','.','.','6','.'},
                {'8','.','.','.','6','.','.','.','3'},
                {'4','.','.','8','.','3','.','.','1'},
                {'7','.','.','.','2','.','.','.','6'},
                {'.','6','.','.','.','.','2','8','.'},
                {'.','.','.','4','1','9','.','.','5'},
                {'.','.','.','.','8','.','.','7','9'}};
        solution36.isValidSudoku(chas);
        
    }
    
}
